package com.link.play.fit.ai.ui.weightcontrol.ac

import android.os.Bundle
import android.view.View
import android.widget.Toast
import androidx.activity.addCallback
import androidx.core.view.isVisible
import androidx.lifecycle.ViewModelProvider
import androidx.lifecycle.lifecycleScope
import com.link.play.fit.ai.R
import com.link.play.fit.ai.base.BaseActivity
import com.link.play.fit.ai.base.goToAc
import com.link.play.fit.ai.databinding.AcWeighControlMethodBinding
import com.link.play.fit.ai.store.CloudStore
import com.link.play.fit.ai.store.CloudStore.DOC_USER_GUIDE
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.delay
import kotlinx.coroutines.flow.collectLatest
import kotlinx.coroutines.flow.update
import kotlinx.coroutines.launch

class WeighControlMethodAc : BaseActivity<AcWeighControlMethodBinding>(AcWeighControlMethodBinding::inflate) {


    val viewModel by lazy(LazyThreadSafetyMode.NONE) {
        ViewModelProvider(this)[WeighControlViewModel::class.java]
    }


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        binding.tvIntermittentFasting.setOnClickListener {
            selectFasting()
        }
        binding.tvCalorieControl.setOnClickListener {
            selectCalorieControl()
        }

        lifecycleScope.launch {
            viewModel.userGuideState.collectLatest {
                viewModel.lastUserGuide = it
                if (it.weighControlMethod == 0) {
                    selectFasting()
                }
                if (it.weighControlMethod == 1) {
                    selectCalorieControl()
                }
            }
        }

        onBackPressedDispatcher.addCallback {
            finish()
        }
        binding.flBack.setOnClickListener {
            onBackPressedDispatcher.onBackPressed()
        }

        binding.tvNext.setOnClickListener {
            when (viewModel.lastUserGuide.weighControlMethod) {
                0 -> {
                    if (viewModel.lastUserGuide.goal == 0 || viewModel.lastUserGuide.goal == 1) {
//                        showTip()
//                        return@setOnClickListener
                    }

                    goToAc(IntermittentFastingAc::class.java)
                    overridePendingTransition(R.anim.slide_in_from_right, R.anim.slide_out_to_left)
                }

                1 -> {
                    goToAc(WeighControlPbAc::class.java)
                    overridePendingTransition(R.anim.slide_in_from_right, R.anim.slide_out_to_left)
                }
            }

            lifecycleScope.launch {
                CloudStore.set(DOC_USER_GUIDE, viewModel.lastUserGuide)
            }

        }
    }

    private fun showTip() {
        if (binding.cardTip.isVisible) {
            return
        }
        binding.cardTip.visibility = View.VISIBLE
        lifecycleScope.launch(Dispatchers.Main) {
            delay(1500)
            binding.cardTip.visibility = View.INVISIBLE
        }
    }

    private fun selectCalorieControl() {
        binding.tvIntermittentFasting.isSelected = false
        binding.tvCalorieControl.isSelected = true
        viewModel.userGuideState.update { it.copy(weighControlMethod = 1) }
        binding.tvNext.isEnabled = true
    }

    private fun selectFasting() {
        binding.tvIntermittentFasting.isSelected = true
        binding.tvCalorieControl.isSelected = false
        viewModel.userGuideState.update { it.copy(weighControlMethod = 0) }
        binding.tvNext.isEnabled = true
    }


}